<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <el-form-item label="序号">
          <el-input
            v-model="searchParam.serialNumber"
            placeholder="请输入序号"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="原产品名称">
          <el-input
            v-model="searchParam.originalProductName"
            placeholder="请输入原产品名称"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="U9铸铁件编码">
          <el-input
            v-model="searchParam.foundryCode"
            placeholder="请输入U9铸铁件编码"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="reset"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-card shadow="never" class="table-container">
      <template #header>
        <!-- <el-button type="success" plain @click="openDialog()"
          ><i-ep-plus />新增</el-button
        > -->
        <el-button
          type="danger"
          plain
          :disabled="ids.length === 0"
          @click="handleTableDelete()"
          ><i-ep-delete />删除</el-button
        >
        <el-button
          class="ml-3"
          @click="
            useDownload('/mes/mesplanconfig/export', '产品目录', searchParam)
          "
        >
          <template #icon><i-ep-download /></template>导出</el-button
        >
        <el-button type="success" @click="excelBoxOpen">导入 </el-button>
        <el-dialog title="产品目录数据导入" v-model="excelBox" width="555px">
          <div>
            <el-button
              @click="
                useDownload(
                  '/mes/mesplanconfig/exportTemp',
                  '产品目录上传模板',
                  searchParam
                )
              "
            >
              <template #icon>
                <i-ep-download />
              </template>
              产品目录上传模板下载
            </el-button>
          </div>
          <div style="margin-top: 10px">
            <input
              class="crop-input"
              type="file"
              name="image"
              accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
              ref="onload_exl"
              @change="getfilepath"
            />
            <el-button
              @click="upload"
              :disabled="uploadBtn"
              style="cursor: pointer"
              >上传</el-button
            >
          </div>
        </el-dialog>
      </template>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column
          label="序号"
          prop="serialNumber"
          min-width="100"
          show-overflow-tooltip
        />

        <template v-for="item in tableColumns" :key="item">
          <TableColumn v-if="item.prop && item.isShow" :column="item">
            <template v-for="slot in Object.keys($slots)" #[slot]="scope">
              <slot :name="slot" v-bind="scope"></slot>
            </template>
          </TableColumn>
        </template>
        <!-- <el-table-column
          label="原产品名称"
          prop="originalProductName"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="ERP系统名称"
          prop="erpSystemName"
          min-width="150"
          show-overflow-tooltip
        />
        <el-table-column
          label="客户代码"
          prop="customerCode"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="品名"
          prop="productName"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="零件号（品号）"
          prop="partNumber"
          min-width="150"
          show-overflow-tooltip
        />
        <el-table-column
          label="图号"
          prop="drawingNo"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="零件上显示号"
          prop="displayNumber"
          min-width="150"
          show-overflow-tooltip
        />
        <el-table-column
          label="产品标准"
          prop="productStandards"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="国标"
          prop="nationalStandard"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="P/M"
          prop="pm"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="模重（KG）"
          prop="modelWeight"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="单重（KG）"
          prop="weight"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="出品率（%）"
          prop="yield"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="负责人"
          prop="kahuna"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="状态"
          prop="state"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="PSW时间"
          prop="pswTime"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="标识位"
          prop="mark"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="U9铸铁件编码"
          prop="foundryCode"
          min-width="150"
          show-overflow-tooltip
        />
        <el-table-column
          label="U9黑件编码"
          prop="blackCode"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="类型名称"
          prop="typeName"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="产出回炉料"
          prop="scrapReturns"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="生产倍量"
          prop="productionMultiplier"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="冷却方式"
          prop="coolingMethod"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="单双缸"
          prop="cylinder"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="量产时间"
          prop="productionTime"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="模具寿命件"
          prop="moldLifespan"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="安全件"
          prop="safetyParts"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="每炉包数"
          prop="furnaceBag"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="单双工位"
          prop="station"
          min-width="100"
          show-overflow-tooltip
        /> -->
        <!-- <el-table-column fixed="right" label="操作" width="240">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialogdetail(scope.row)"
              ><i-ep-search />
              查看
            </el-button>
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialog(scope.row)"
              ><i-ep-edit />编辑</el-button
            >
            <el-button
              type="primary"
              size="small"
              link
              @click="deleteItem(scope.row.id)"
              ><i-ep-delete />删除</el-button
            >
          </template>
        </el-table-column> -->
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <mesplanconfig-edit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
    <!-- 列设置 -->
    <ColSetting
      v-model:col-setting="tableColumns"
      v-model:search-setting="searchColumns"
      @search-handle="(e:boolean) => (showSearch = e)"
    />
  </div>
</template>

<script setup lang="ts">
import { useTable, useDownload, useDictionary } from "@/utils/useTable";
import request from "@/utils/request";

defineOptions({
  name: "MESMESPLANCONFIG",
  inheritAttrs: false,
});

// 请求参数
const requestApi = "/mes/mesplanconfig/queryByPage";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  serialNumber: "",
  originalProductName: "",
  erpSystemName: "",
  productName: "",
  partNumber: "",
  drawingNo: "",
  nationalStandard: "",
  state: "",
  pswTimeSearch: [],
  pswTimeStart: "",
  pswTimeEnd: "",
  foundryCode: "",
  blackCode: "",
  typeName: "",
  coolingMethod: "",
  cylinder: "",
  productionTimeSearch: [],
  productionTimeStart: "",
  productionTimeEnd: "",
  createTimeSearch: [],
  createTimeStart: "",
  createTimeEnd: "",
  lastUpdateTimeSearch: [],
  lastUpdateTimeStart: "",
  lastUpdateTimeEnd: "",
  enabled: "",
  deleted: "",
};

const deleteParam = {
  api: "/mes/mesplanconfig/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

onMounted(() => {
  getTableList();
});

const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});

// 查询详情
async function getInfo(id: string) {
  request({
    url: `/mes/mesplanconfig/get/${id}`,
    method: "get",
  })
    .then((data) => {
      addDialog.data = data.data;
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

/** 打开产品目录表单弹窗 */
function openDialog(roleObj?: Object) {
  if (roleObj) {
    addDialog.title = "修改产品目录";
    getInfo(roleObj.id);
  } else {
    addDialog.title = "新增产品目录";
    addDialog.visible = true;
  }
  addDialog.readvisible = false;
}

/** 打开产品目录表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  addDialog.title = "产品目录详情";
  getInfo(roleObj.id);
  addDialog.readvisible = true;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}

function getpswTime(data: any) {
  if (data) {
    searchParam.value.pswTimeStart = data[0];
    searchParam.value.pswTimeEnd = data[1];
  } else {
    searchParam.value.pswTimeStart = "";
    searchParam.value.pswTimeEnd = "";
  }
}
function getproductionTime(data: any) {
  if (data) {
    searchParam.value.productionTimeStart = data[0];
    searchParam.value.productionTimeEnd = data[1];
  } else {
    searchParam.value.productionTimeStart = "";
    searchParam.value.productionTimeEnd = "";
  }
}
function getcreateTime(data: any) {
  if (data) {
    searchParam.value.createTimeStart = data[0];
    searchParam.value.createTimeEnd = data[1];
  } else {
    searchParam.value.createTimeStart = "";
    searchParam.value.createTimeEnd = "";
  }
}
function getlastUpdateTime(data: any) {
  if (data) {
    searchParam.value.lastUpdateTimeStart = data[0];
    searchParam.value.lastUpdateTimeEnd = data[1];
  } else {
    searchParam.value.lastUpdateTimeStart = "";
    searchParam.value.lastUpdateTimeEnd = "";
  }
}

// 导入excel
const uploadApi = "/mes/mesplanconfig/upload";
const excelBox = ref(false);
const uploadBtn = ref(false);
const uploadFile = ref("");
const fileName = ref("");

function excelBoxOpen() {
  excelBox.value = true;
}

function getfilepath(e: any) {
  const file = e.target.files[0];
  fileName.value = file.name;
  uploadFile.value = file;
  uploadBtn.value = false;
}

function upload() {
  if (!uploadFile.value) {
    ElMessage({
      type: "warning",
      message: "请选择文件！",
    });
    return;
  }

  let formdata = new FormData();
  formdata.append("file", uploadFile.value);
  let config = { headers: { "Content-Type": "multipart/form-data" } };
  request
    .post(uploadApi, formdata, config)
    .then((res) => {
      ElMessage({
        type: "success",
        message: "上传成功！",
      });

      excelBox.value = false;
      uploadBtn.value = true;
      search();
    })
    .catch((error) => {
      console.log(error);
    });
}

async function deleteItem(id: any) {
  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    const { data } = await request({
      url: `/mes/mesplanconfig/deleteItem/${id}`,
      method: "post",
    });
    search();
  });
}

const tableColumns = reactive([
  {
    prop: "originalProductName",
    label: "原产品名称",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "erpSystemName",
    label: "ERP系统名称",
    minWidth: 150,
    isShow: true,
    align: "left",
  },
  {
    prop: "customerCode",
    label: "客户代码",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "productName",
    label: "品名",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "partNumber",
    label: "零件号（品号）",
    minWidth: 150,
    isShow: true,
    align: "left",
  },
  {
    prop: "drawingNo",
    label: "图号",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "displayNumber",
    label: "零件上显示号",
    minWidth: 150,
    isShow: true,
    align: "left",
  },
  {
    prop: "productStandards",
    label: "产品标准",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "nationalStandard",
    label: "国标",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "pm",
    label: "P/M",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "modelWeight",
    label: "模重（KG）",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "weight",
    label: "单重（KG）",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "yield",
    label: "出品率（%）",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "kahuna",
    label: "负责人",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "state",
    label: "状态",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "pswTime",
    label: "PSW时间",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "mark",
    label: "标识位",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "foundryCode",
    label: "U9铸铁件编码",
    minWidth: 150,
    isShow: true,
    align: "left",
  },
  {
    prop: "blackCode",
    label: "U9黑件编码",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "typeName",
    label: "类型名称",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "scrapReturns",
    label: "产出回炉料",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "productionMultiplier",
    label: "生产倍量",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "coolingMethod",
    label: "冷却方式",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "cylinder",
    label: "单双缸",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "productionTime",
    label: "量产时间",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "moldLifespan",
    label: "模具寿命件",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "safetyParts",
    label: "安全件",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "furnaceBag",
    label: "每炉包数",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "station",
    label: "单双工位",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
]);

const searchColumns = reactive([]);
</script>
